from django.core.management.base import BaseCommand
from django.contrib.auth.models import Group, Permission


class Command(BaseCommand):
    help = '创建默认用户角色'

    def handle(self, *args, **options):
        manager, _ = Group.objects.get_or_create(name='Manager')
        member, _ = Group.objects.get_or_create(name='Member')

        # 添加权限
        book_perms = Permission.objects.filter(content_type__app_label='books')
        manager.permissions.set(book_perms)

        borrow_perm = Permission.objects.get(codename='can_borrow_books')
        member.permissions.add(borrow_perm)